/*
 * Name        : HttpRequest.java
 * Classes     : HttpRequest
 * Version     : 1.0
 * Date        : 2013-08-29
 *
 * Copyright 2013 CMCC.  All rights reserved.
 * This material, including documentation and any related computer
 * programs, is protected by copyright controlled by CMCC.  All
 * rights are reserved.  Copying, including reproducing, storing,
 * adapting or translating, any or all of this material requires the
 * prior written consent of CMCC.  This material also contains
 * confidential information which may not be disclosed to others
 * without the prior written consent of CMCC.
 */

package com.xgkj.diyiketang.http;


import java.util.Map;


/**
 * Class Name  : HttpRequest
 * Author      : Zhang Lei
 * Date        : 13-9-5
 * Description : send http request with type get or post, then handle HttpEvent from HttpEngine.
 */
public class HttpRequest extends BaseRequest implements HttpEventListener{
    public enum HttpMethodType {
        HTTP_METHOD_GET,
        HTTP_METHOD_POST
    }

    private HttpEngine mHttpEngine;
    private String mUrl;
    private Map<String, String> mFormParam;
    private Map<String ,String> mHttpHeadParams;
    private byte[] mData;
    private HttpMethodType mHttpMethodType;
    
    /**
     * Constructor without parameter
     */
    public HttpRequest() {
        this(HTTP_REQUEST_TYPE);
    }

    /**
     * Constructor with parameter
     *
     * @param requestType, for http request type, get or post.
     */
    protected HttpRequest(int requestType) {
        this(requestType, null);

    }
    
    /**
     * Constructor with HttpEngine.
     * @param httpEngine for send HTTP request.
     */
    public HttpRequest(HttpEngine httpEngine){
    	this(HTTP_REQUEST_TYPE, httpEngine);
    }
    
    /**
     * Constructor with request type and HttpEngine.
     *
     * @param requestType, for http request type, get or post.
     * @param httpEngine for send HTTP request.
     */
    public HttpRequest(int requestType,HttpEngine httpEngine){
    	super(requestType);
    	super.mResponse = new HttpResponse();
        if (httpEngine != null) {
            mHttpEngine = httpEngine;
        } else {
            mHttpEngine = HttpEngineFactory.createHttpEngine();
        }

    }
    
    public void setData(byte[] data) {
    	mData = data;
    }

    public void setHttpMethod(HttpMethodType method) {
        mHttpMethodType = method;
    }

    public void setUrl(String url) {
        mUrl = url;
    }
    
    public void setHttpHeaderParam(Map<String ,String> httpHeadParams) {
    	mHttpHeadParams = httpHeadParams;
    }

    public void setFormParam(Map<String, String> param) {
    	mFormParam = param;
    }

    /**
     * start http request with type get or post.
     */
    @Override
    public void startRequest() {
    	if (mHttpMethodType == null) {
            throw new NullPointerException("HttpRequest: HttpMethodType is null, please set one type!");
        }

    	mHttpEngine.addHttpListener(this);
    	switch (mHttpMethodType) {
		case HTTP_METHOD_GET:
			mHttpEngine.sendGet(mUrl, mHttpHeadParams);
			break;
		case HTTP_METHOD_POST:
			mHttpEngine.sendPost(mUrl, mData, mFormParam, mHttpHeadParams);
			break;
		default:
			break;
		}
    }

    /**
     * Inherit from HttpEventListener, handle HttpEvent which returned from HttpEngine.
     */
    @Override
    public void handleHttpEvent(HttpEvent event) {
    	super.mResponse.handleResponseData(event);
    	mHttpEngine.removeHttpListener(this);
    	super.notifyListener();
    }
}
